package hu.tortavar.client.ui;

import java.util.List;

import hu.tortavar.client.Termek;
import hu.tortavar.client.TermekService;
import hu.tortavar.client.TermekServiceAsync;
import hu.tortavar.client.data.Csoportok;
import hu.tortavar.client.data.HistoryToken;
import hu.tortavar.client.model.MenuCommand;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Float;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;

import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.MenuItem;
import com.google.gwt.user.client.ui.VerticalPanel;

public class Main extends Composite {
	

	private static MainUiBinder uiBinder = GWT.create(MainUiBinder.class);

	interface MainUiBinder extends UiBinder<VerticalPanel, Main> {
	}
	
	TermekServiceAsync termek = GWT.create(TermekService.class);
	
	@UiField Image map;
	@UiField HorizontalPanel fejlec;
	@UiField HorizontalPanel hir;
	@UiField HorizontalPanel lablec;
	@UiField Image logoImage;
	
	@UiField MenuItem mTortaSzelet;
	@UiField MenuItem mTortaEgesz;
	@UiField MenuItem mFormaTorta;
	@UiField MenuItem mAlkalmiTorta;
	@UiField MenuItem mAproSutemeny;
	@UiField MenuItem mRetesek;
	@UiField MenuItem mKremesek;
	@UiField MenuItem mSutemenyek;
	@UiField MenuItem mRolunk;
	//@UiField FlexTable main_list;
	
	HorizontalPanel teszt = new HorizontalPanel();
	Label tesztLabel = new Label("Hello");
	FlexTable listContainer = new FlexTable();
	
	
	MenuCommand cmdHir = new MenuCommand(HistoryToken.Hir);
	MenuCommand cmdTortaSzelet = new MenuCommand(HistoryToken.Szelet);
	MenuCommand cmdTortaEgesz = new MenuCommand(HistoryToken.Egesztorta);
	MenuCommand cmdFormaTorta= new MenuCommand(HistoryToken.Forma);
	MenuCommand cmdAlkalmiTorta = new MenuCommand(HistoryToken.Alkalmi);
	MenuCommand cmdAproSutemeny = new MenuCommand(HistoryToken.Apro);
	MenuCommand cmdRetesek = new MenuCommand(HistoryToken.Retes);
	MenuCommand cmdKremes = new MenuCommand(HistoryToken.Kremes);
	MenuCommand cmdSutemeny = new MenuCommand(HistoryToken.Sutemeny);
	MenuCommand cmdRolunk = new MenuCommand(HistoryToken.Rolunk);
	
	public Main() {
		initWidget(uiBinder.createAndBindUi(this));
		map.setUrl("http://maps.googleapis.com/maps/api/staticmap?center=47.510143,19.025788&zoom=15&format=png&sensor=false" +
				"&size=440x240&markers=icon:"+ GWT.getHostPageBaseURL() +"images/marker.png%7C25+budapest+keleti+karoly+street+1024+hungary&maptype=roadmap" +
				"&style=hue:0xff9100&style=feature:poi.business|element:labels|visibility:off");
	
		mTortaSzelet.setCommand(cmdTortaSzelet);
		mFormaTorta.setCommand(cmdFormaTorta);
		mTortaEgesz.setCommand(cmdTortaEgesz);
		mAlkalmiTorta.setCommand(cmdAlkalmiTorta);
		mAproSutemeny.setCommand(cmdAproSutemeny);
		mRetesek.setCommand(cmdRetesek);
		mKremesek.setCommand(cmdKremes);
		mSutemenyek.setCommand(cmdSutemeny);
		mRolunk.setCommand(cmdRolunk);
		
		logoImage.addStyleDependentName("link");
		logoImage.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				// TODO Auto-generated method stub
				History.newItem(HistoryToken.Hir.toString());
				//Window.open(GWT.getHostPageBaseURL(), "_self", "");
			}
		});
				
//				termek.addTermek("Forma 1", "", "/images/cakes/kremes.png", 10, false, "123", new AsyncCallback<Void>() {
//					
//					@Override
//					public void onSuccess(Void result) {
//						// TODO Auto-generated method stub
//						
//					}
//					
//					@Override
//					public void onFailure(Throwable caught) {
//						// TODO Auto-generated method stub
//						
//					}
//				});
		
		History.addValueChangeHandler(new ValueChangeHandler<String>() {
			
			@Override
			public void onValueChange(ValueChangeEvent<String> event) {
				// TODO Auto-generated method stub
				String historyToken = event.getValue();
				
				switch (HistoryToken.valueOf(historyToken)) {
				case Hir:
					getNews();
					break;
				case Szelet:	getTortak(Csoportok.SZELET);			break;
				case Egesztorta:getTortak(Csoportok.EGESZTORTA);		break;
				case Forma:		getTortak(Csoportok.FORMA);				break;
				case Alkalmi:	getTortak(Csoportok.ESEMENY);			break;
				case Apro:		getTortak(Csoportok.APRO);				break;
				case Retes:		getTortak(Csoportok.RETES);				break;
				case Kremes:	getTortak(Csoportok.KREMES);			break;
				case Sutemeny:	getTortak(Csoportok.SUTEMENYEK);		break;
				case Rolunk:	getRolunk();							break;
				default:
					break;
				}
			}
		});
		getNews();
		//History.newItem(HistoryToken.Hir.toString());
	}
/*	private void getTermekcsoport(int csoport) {
		hir.clear();
		//listContainer.clear();
		listContainer.removeAllRows();
		hir.add(new Betoltes());
		termek.getTermekek(csoport, new AsyncCallback<List<Termek>>() {
			
			@Override
			public void onSuccess(List<Termek> termekek) {
				hir.clear();
				hir.add(listContainer);
				for (Termek termek : termekek) {
				int row = listContainer.getRowCount();
				listContainer.setWidget(row, 0, new ListaRenderHTML(termek.getCim(), termek.getLeiras(), termek.getAr()));
				listContainer.setWidget(row, 1, new Image(termek.getImageUrl()));
				//plus
				hir.addStyleName("flowerBg");
				}
			System.out.println(termekek.size()+" db");
			}			
			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub	
			}
		});
	}*/
	
	private void getTortak(int csoport) {
		hir.clear();
		final FlowPanel container = new FlowPanel();
		container.setWidth("100%");
		//container.getElement().setClassName("teszt");
		container.getElement().getStyle().setFloat(Float.LEFT);
		hir.add(new Betoltes());
				termek.getTermekek(csoport, new AsyncCallback<List<Termek>>() {
			
			@Override
			public void onSuccess(List<Termek> termekek) {
				hir.clear();
				hir.add(container);
				for (Termek termek : termekek) {
					EgeszTortakViewImpl view = new EgeszTortakViewImpl();
					view.setEgeszTortaElement(termek.getImageUrl(), termek.getCim(), termek.getAr());
					container.add(view);
				}
			}
			
			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}
		});
	}

	private void getNews() {
		hir.clear();
		hir.add(new Betoltes());
		termek.getNews(new AsyncCallback<List<Termek>>() {
			
			@Override
			public void onSuccess(List<Termek> hirek) {
				hir.clear();
				Termek hir1  = hirek.get(0);
				// TODO Auto-generated method stub
				//main_list.setVisible(false);
				hir.add(new NewsRender(hir1.getImageUrl(), hir1.getCim(), hir1.getLeiras()));
			}
			
			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				Window.alert("Hiba, kivétel");
			}
		});
	}
	
	private void getRolunk() {
		hir.clear();
		hir.add(new RolunkViewImpl());
	}
	
}
